package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //新增检查组数据
        checkGroupDao.add(checkGroup);
        //增加检查组检查项关系\
        Integer checkGroupId = checkGroup.getId();
        this.setcheckgroupandcheckitem(checkGroupId,checkitemIds);
    }

//    分页管理
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        //        mybatis 分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = checkGroupDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }
//回写数据
    @Override
    public CheckGroup copyback(Integer id) {
        CheckGroup checkGroup = checkGroupDao.copyback(id);
        return checkGroup;
    }
    //回写数据检查项信息
    @Override
    public List<Integer> copybackids(Integer id) {
        List<Integer> integerList=checkGroupDao.copybackids(id);
        return integerList;
    }

    //编辑检查组
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //修改检查组信息
           checkGroupDao.edit(checkGroup);
       //将关系进行清除
           checkGroupDao.deleteAssociation(checkGroup.getId());
        //重新设置关联关系
        Integer checkGroupId = checkGroup.getId();
        this.setcheckgroupandcheckitem(checkGroupId,checkitemIds);
    }
//删除检查组
    @Override
    public void del(Integer id) {
        //首先删除中间表关联关系
        checkGroupDao.deleteAssociation(id);
        //删除检查组表
        checkGroupDao.del(id);
    }
//查询检查组
    @Override
    public List<CheckGroup> findallitem() {
        List<CheckGroup> groupList=checkGroupDao.findall();
        return groupList;
    }

    //    抽取
    public  void  setcheckgroupandcheckitem(Integer checkGroupId,Integer[] checkitemIds){
        if (checkitemIds !=null && checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkitem_id",checkitemId);
                map.put("checkgroup_id",checkGroupId);
                checkGroupDao.setcheckgroupandcheckitem(map);
            }
        }
    }
}
